package triggerhelper.dataHolders;

import java.util.ArrayList;
import javax.swing.*;

public class Trigger extends SimpleTrigger {

    private ArrayList<Variable> variables;
    private ArrayList<Variable> unsavedVariables;
    private String actions, conditions;
    private int numIterations;

    public Trigger(String name_) {
        super(name_);
        conditions = startCond;
        actions = startAction;
        variables = new ArrayList<Variable>();
        unsavedVariables = new ArrayList<Variable>();
        numIterations = 1;
    }

    public void addVariable(Variable var){
        System.out.println("var is added");
        variables.add(var);
        isSaved = false;
    }
    public void addUnsavedVariable(Variable var){
        unsavedVariables.add(var);
        isSaved = false;
    }
    public void removeVariable(Variable var){
        boolean found = variables.remove(var);
        if(!found){
            found = unsavedVariables.remove(var);
            if(!found){
                System.out.println("ERROR - remove var not found");
            } else {
                isSaved = false;
            }
        }
    }
    public void changeVariable(String previousName, Variable replacement){
        boolean found = false;
        int count = 0;
        while(!found && count < variables.size()){
            if(variables.get(count).getName().equals(previousName)){
                found = true;
                variables.set(count, replacement);
                isSaved = false;
            }else if(unsavedVariables.get(count).getName().equals(previousName)){
                found = true;
                unsavedVariables.set(count, replacement);
                isSaved = false;
            }
            count++;
        }
        if(!found){
            JOptionPane.showMessageDialog(null, "Error - there was no variable found which previously had given name.");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Getter methods">
    public String getActions() {
        return actions;
    }public String getConditions() {
        return conditions;
    }public int getNumIterations() {
        return numIterations;
    }public ArrayList<Variable> getVariables() {
        return variables;
    }public ArrayList<Variable> getUnsavedVariables() {
        return unsavedVariables;
    }@Override
    public boolean isSimple() {
        return false;
    }// </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Setter methods">
    public void setActions(String s) {
        if (!actions.equals(s)) {
            actions = s;
            isSaved = false;
        }
    }

    public void setConditions(String s) {
        if (!conditions.equals(s)) {
            conditions = s;
            isSaved = false;
        }
    }
    public void setNumIterations(int n) {
        if (numIterations != n) {
            numIterations = n;
            isSaved = false;
        }
    }// </editor-fold>
}
